﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using HeartLess.LevelData;
using HeartLess.Managers;
using HeartLess.Core;
using HeartLess.GameObjects;

namespace HeartLess.Activities
{
    public class GameActivity : Activity
    {
        private Level _currentLevel;
        private int _currentId;
        private Hud _hud;

        private bool _clearingLevel;

        public static GameActivity Instance;

        public GameActivity()
        {
            Instance = this;

            InputManager.Input.DirectionalRepeatAllowed = true;

            _hud = new Hud();

            _currentId = 1;
            _currentLevel = LevelLoader.LoadLevel(_currentId);
        }

        public override void Update(GameTime gameTime)
        {
            if (_clearingLevel)
            {
                ShaderManager.clearShaderDrawers();
                CollidableGameObject.Unload();
                if (_currentId > 2)
                {
                    PauseGame();
                    _currentId = 1;
                }
                _currentLevel = LevelLoader.LoadLevel(_currentId);
                _clearingLevel = false;
            }

            if (InputManager.Input.IsPressingBack 
                || HeartlessGame.Game.IsActive == false)
            {
                PauseGame();
                return;
            }

            _currentLevel.Update(gameTime);

            _hud.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            _currentLevel.Draw(spriteBatch);

            _hud.Draw(spriteBatch);
        }

        public Hud Hud
        {
            get
            {
                return _hud;
            }
        }

        public void PauseGame()
        {
            ActivityManager.CurrentActivity = new PauseActivity(this);
        }

        public void RestartLevel()
        {
            if (_clearingLevel == false)
            {
                _clearingLevel = true;
            }
        }

        public void StartNextLevel()
        {
            if (_clearingLevel == false)
            {
                _currentId++;
                _clearingLevel = true;
            }
        }
    }
}
